from dataclasses import dataclass, field
from typing import List


@dataclass
class Address:
    city: str
    province: str

@dataclass
class Commodity:
    row: str
    word: str


@dataclass
class Invoice:
    purchaser_address: str
    purchaser_bank: str
    password: str
    commodity_vehicle_type: List[str]
    seller_register_num: str
    seller_bank:str
    commodity_num: List[Commodity]
    commodity_amount: List[Commodity]
    invoice_type: str
    amount_in_words: str
    total_tax: str
    machine_code: str
    city: str
    invoice_num_digit: str
    checker: str
    invoice_code: str
    seller_address: str
    commodity_price: List[Commodity]
    note_drawer: str
    province: str
    invoice_num: str
    commodity_tax_rate: List[Commodity]
    service_type: str
    invoice_date: str
    commodity_end_date: List[str]
    purchaser_register_num: str
    commodity_start_date: List[str]
    total_amount: str
    sheet_num: str
    commodity_plate_num: List[str]
    purchaser_name: str
    seller_name: str
    invoice_num_confirm: str
    agent: str
    invoice_tag: str
    commodity_unit: List[str]
    check_code: str
    invoice_type_org: str
    invoice_code_confirm: str
    remarks: str
    payee: str
    commodity_tax: List[Commodity]
    amount_in_figures: str
    commodity_name: List[Commodity]
    commodity_type: List[str]
    online_pay: str

# 转换字典数据为Python对象
def dict_to_invoice(invoiceObj,arr):
    dictArr = []
    for i in range(len(arr)):
        commodity_num = [Commodity(**item) for item in arr[i]['CommodityNum']]
        commodity_amount = [Commodity(**item) for item in arr[i]['CommodityAmount']]
        commodity_price = [Commodity(**item) for item in arr[i]['CommodityPrice']]
        commodity_tax_rate = [Commodity(**item) for item in arr[i]['CommodityTaxRate']]
        commodity_tax = [Commodity(**item) for item in arr[i]['CommodityTax']]
        commodity_name = [Commodity(**item) for item in arr[i]['CommodityName']]

        invoice = Invoice(
            purchaser_address=arr[i]['PurchaserAddress'],
            purchaser_bank=arr[i]['PurchaserBank'],
            password=arr[i]['Password'],
            commodity_vehicle_type=arr[i]['CommodityVehicleType'],
            seller_register_num=arr[i]['SellerRegisterNum'],
            seller_bank=arr[i]['SellerBank'],
            commodity_num=commodity_num,
            commodity_amount=commodity_amount,
            invoice_type=arr[i]['InvoiceType'],
            amount_in_words=arr[i]['AmountInWords'],
            total_tax=arr[i]['TotalTax'],
            machine_code=arr[i]['MachineCode'],
            city=arr[i]['City'],
            invoice_num_digit=arr[i]['InvoiceNumDigit'],
            checker=arr[i]['Checker'],
            invoice_code=arr[i]['InvoiceCode'],
            seller_address=arr[i]['SellerAddress'],
            commodity_price=commodity_price,
            note_drawer=arr[i]['NoteDrawer'],
            province=arr[i]['Province'],
            invoice_num=arr[i]['InvoiceNum'],
            commodity_tax_rate=commodity_tax_rate,
            service_type=arr[i]['ServiceType'],
            invoice_date=arr[i]['InvoiceDate'],
            commodity_end_date=arr[i]['CommodityEndDate'],
            purchaser_register_num=arr[i]['PurchaserRegisterNum'],
            commodity_start_date=arr[i]['CommodityStartDate'],
            total_amount=arr[i]['TotalAmount'],
            sheet_num=arr[i]['SheetNum'],
            commodity_plate_num=arr[i]['CommodityPlateNum'],
            purchaser_name=arr[i]['PurchaserName'],
            seller_name=arr[i]['SellerName'],
            invoice_num_confirm=arr[i]['InvoiceNumConfirm'],
            agent=arr[i]['Agent'],
            invoice_tag=arr[i]['InvoiceTag'],
            commodity_unit=arr[i]['CommodityUnit'],
            check_code=arr[i]['CheckCode'],
            invoice_type_org=arr[i]['InvoiceTypeOrg'],
            invoice_code_confirm=arr[i]['InvoiceCodeConfirm'],
            remarks=arr[i]['Remarks'],
            payee=arr[i]['Payee'],
            commodity_tax=commodity_tax,
            amount_in_figures=arr[i]['AmountInFiguers'],
            commodity_name=commodity_name,
            commodity_type=arr[i]['CommodityType'],
            online_pay=arr[i]['OnlinePay']
        )
        dictArr.append(invoice)
    return data_Excel(invoiceObj,dictArr)


def data_Excel(invoices,arr):
    result = {}
    for invoice in arr:
        result.setdefault("InvoiceTypeOrg", []).append(invoice.invoice_type_org)
        result.setdefault("InvoiceCodeConfirm", []).append(invoice.invoice_code_confirm)
        result.setdefault("InvoiceNumConfirm", []).append(invoice.invoice_num_confirm)
        result.setdefault("InvoiceDate", []).append(invoice.invoice_date)
        result.setdefault("CheckCode", []).append(invoice.check_code)
        result.setdefault("MachineCode", []).append(invoice.machine_code)
        result.setdefault("PurchaserName", []).append(invoice.purchaser_name)
        result.setdefault("PurchaserRegisterNum", []).append(invoice.purchaser_register_num)
        result.setdefault("Password", []).append(invoice.password)
        result.setdefault("CommodityName", []).append(invoice.commodity_name[0].word)
        result.setdefault("CommodityPrice", []).append(invoice.commodity_price[0].word)
        result.setdefault("CommodityAmount", []).append(invoice.commodity_amount[0].word)
        result.setdefault("CommodityTaxRate", []).append(invoice.commodity_tax_rate[0].word)
        result.setdefault("CommodityTax", []).append(invoice.commodity_tax[0].word)
        result.setdefault("TotalTax", []).append(invoice.total_tax)
        result.setdefault("AmountInWords", []).append(invoice.amount_in_words)
        result.setdefault("TotalAmount", []).append(invoice.total_amount)
        result.setdefault("SellerName", []).append(invoice.seller_name)
        result.setdefault("SellerRegisterNum", []).append(invoice.seller_register_num)
        result.setdefault("SellerAddress", []).append(invoice.seller_address)
        result.setdefault("SellerBank", []).append(invoice.seller_bank)
        result.setdefault("Remarks", []).append(invoice.remarks)
        result.setdefault("Payee", []).append(invoice.payee)
        result.setdefault("Checker", []).append(invoice.checker)
        result.setdefault("NoteDrawer", []).append(invoice.note_drawer)
    return result

# 提供的字典数据
data = {
    'PurchaserAddress': '',
    'PurchaserBank': '',
    'Password': '0-57*+2/6<08-3534>9-9695>1-595++518-68+-97<19>67*19<*/01907>5<>>-88105-3166<839095<973-7-<94++74<+06*15-4<45',
    'CommodityVehicleType': [],
    'SellerRegisterNum': '91330100552660066F',
    'SellerBank': '上海浦东发展银行杭州高新支行 95130154800003964',
    'CommodityNum': [{'row': '1', 'word': '1'}],
    'CommodityAmount': [{'row': '1', 'word': '185.93'}],
    'InvoiceType': '电子普通发票',
    'AmountInWords': '壹佰捌拾陆圆整',
    'TotalTax': '0.07',
    'MachineCode': '661620034630',
    'City': '',
    'InvoiceNumDigit': '',
    'Checker': '陈焕生',
    'InvoiceCode': '033002000111',
    'SellerAddress': '杭州市江干区钱潮路369号智谷人才广场8楼0571-87233885',
    'CommodityPrice': [{'row': '1', 'word': '185.93'}],
    'NoteDrawer': '刘荣佑',
    'Province': '浙江省',
    'InvoiceNum': '74172194',
    'CommodityTaxRate': [{'row': '1', 'word': '***'}],
    'ServiceType': '服务',
    'InvoiceDate': '2020年06月12日',
    'CommodityEndDate': [],
    'PurchaserRegisterNum': '91310105572715984J',
    'CommodityStartDate': [],
    'TotalAmount': '186.00',
    'SheetNum': '',
    'CommodityPlateNum': [],
    'PurchaserName': '上海艺赛旗软件股份有限公司',
    'SellerName': '浙江外企德科人力资源服务有限公司',
    'InvoiceNumConfirm': '74172194',
    'Agent': '否',
    'InvoiceTag': '其他',
    'CommodityUnit': [],
    'CheckCode': '79585562461837809249',
    'InvoiceTypeOrg': '浙江增值税电子普通发票',
    'InvoiceCodeConfirm': '033002000111',
    'Remarks': '差额征税:184.51。',
    'Payee': '俞兰',
    'CommodityTax': [{'row': '1', 'word': '0.07'}],
    'AmountInFiguers': '186.00',
    'CommodityName': [{'row': '1', 'word': '*人力资源服务*劳务费-代驾'}],
    'CommodityType': [],
    'OnlinePay': ''
}


"""
class Address:
    def __init__(self, city, province):
        self.city = city
        self.province = province

    def __repr__(self):
        return f'Address(city={self.city}, province={self.province})'

class BankInfo:
    def __init__(self, bank_name, account_number):
        self.bank_name = bank_name
        self.account_number = account_number

    def __repr__(self):
        return f'BankInfo(bank_name={self.bank_name}, account_number={self.account_number})'

class Commodity:
    def __init__(self, row, word, price, amount, tax, tax_rate):
        self.row = row
        self.word = word
        self.price = price
        self.amount = amount
        self.tax = tax
        self.tax_rate = tax_rate

    def __repr__(self):
        return f'Commodity(row={self.row}, word={self.word}, price={self.price}, amount={self.amount}, tax={self.tax}, tax_rate={self.tax_rate})'

class Invoice:
    def __init__(self, purchaser_address, purchaser_bank, password, commodity_vehicle_type, seller_register_num, seller_bank, commodity_num, commodity_amount, invoice_type, amount_in_words, total_tax, machine_code, city, invoice_num_digit, checker, invoice_code, seller_address, commodity_price, note_drawer, province, invoice_num, commodity_tax_rate, service_type, invoice_date, commodity_end_date, purchaser_register_num, commodity_start_date, total_amount, sheet_num, commodity_plate_num, purchaser_name, seller_name, invoice_num_confirm, agent, invoice_tag, commodity_unit, check_code, invoice_type_org, invoice_code_confirm, remarks, payee, commodity_tax, amount_in_figures, commodity_name, commodity_type, online_pay):
        self.purchaser_address = purchaser_address
        self.purchaser_bank = purchaser_bank
        self.password = password
        self.commodity_vehicle_type = commodity_vehicle_type
        self.seller_register_num = seller_register_num
        self.seller_bank = seller_bank
        self.commodity_num = commodity_num
        self.commodity_amount = commodity_amount
        self.invoice_type = invoice_type
        self.amount_in_words = amount_in_words
        self.total_tax = total_tax
        self.machine_code = machine_code
        self.city = city
        self.invoice_num_digit = invoice_num_digit
        self.checker = checker
        self.invoice_code = invoice_code
        self.seller_address = seller_address
        self.commodity_price = commodity_price
        self.note_drawer = note_drawer
        self.province = province
        self.invoice_num = invoice_num
        self.commodity_tax_rate = commodity_tax_rate
        self.service_type = service_type
        self.invoice_date = invoice_date
        self.commodity_end_date = commodity_end_date
        self.purchaser_register_num = purchaser_register_num
        self.commodity_start_date = commodity_start_date
        self.total_amount = total_amount
        self.sheet_num = sheet_num
        self.commodity_plate_num = commodity_plate_num
        self.purchaser_name = purchaser_name
        self.seller_name = seller_name
        self.invoice_num_confirm = invoice_num_confirm
        self.agent = agent
        self.invoice_tag = invoice_tag
        self.commodity_unit = commodity_unit
        self.check_code = check_code
        self.invoice_type_org = invoice_type_org
        self.invoice_code_confirm = invoice_code_confirm
        self.remarks = remarks
        self.payee = payee
        self.commodity_tax = commodity_tax
        self.amount_in_figures = amount_in_figures
        self.commodity_name = commodity_name
        self.commodity_type = commodity_type
        self.online_pay = online_pay

    def __repr__(self):
        return f'Invoice(purchaser_address={self.purchaser_address}, purchaser_bank={self.purchaser_bank}, password={self.password}, commodity_vehicle_type={self.commodity_vehicle_type}, seller_register_num={self.seller_register_num}, seller_bank={self.seller_bank}, commodity_num={self.commodity_num}, commodity_amount={self.commodity_amount}, invoice_type={self.invoice_type}, amount_in_words={self.amount_in_words}, total_tax={self.total_tax}, machine_code={self.machine_code}, city={self.city}, invoice_num_digit={self.invoice_num_digit}, checker={self.checker}, invoice_code={self.invoice_code}, seller_address={self.seller_address}, commodity_price={self.commodity_price}, note_drawer={self.note_drawer}, province={self.province}, invoice_num={self.invoice_num}, commodity_tax_rate={self.commodity_tax_rate}, service_type={self.service_type}, invoice_date={self.invoice_date}, commodity_end_date={self.commodity_end_date}, purchaser_register_num={self.purchaser_register_num}, commodity_start_date={self.commodity_start_date}, total_amount={self.total_amount}, sheet_num={self.sheet_num}, commodity_plate_num={self.commodity_plate_num}, purchaser_name={self.purchaser_name}, seller_name={self.seller_name}, invoice_num_confirm={self.invoice_num_confirm}, agent={self.agent}, invoice_tag={self.invoice_tag}, commodity_unit={self.commodity_unit}, check_code={self.check_code}, invoice_type_org={self.invoice_type_org}, invoice_code_confirm={self.invoice_code_confirm}, remarks={self.remarks}, payee={self.payee}, commodity_tax={self.commodity_tax}, amount_in_figures={self.amount_in_figures}, commodity_name={self.commodity_name}, commodity_type={self.commodity_type}, online_pay={self.online_pay})'

"""


